<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar">
      <el-form :inline="true" :model="filters">
        <el-row class="search-input">
          <el-form-item label="ID：">
            <el-input type="text" oninput="value=value.replace(/\D/g,'')" v-model="filters.id" clearable
                      placeholder="ID搜索"></el-input>
          </el-form-item>
          <el-form-item label="名称：">
            <el-input v-model="filters.title" clearable placeholder="名称搜索"></el-input>
          </el-form-item>
          <el-form-item label="状态：">
            <el-select v-model="filters.status">
              <el-option label="草稿" value="0">草稿</el-option>
              <el-option label="上架" value="1">上架</el-option>
              <el-option label="下架" value="2">下架</el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="修改时间：" prop="updateTimeStart">
            <el-date-picker
              v-model="filters.updateTimeStart"
              type="daterange"
              range-separator="~"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>

        </el-row>
        <el-row class="search-button">
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(filters)"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-refresh" :label="$t('action.reset')"
              @click="reset"
              type="primary"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-hand-pointer-o"
              :label="$t('action.add')"
              @click="handleAdd"
              type="primary"
            />
          </el-form-item>
        </el-row>
        <el-form-item class="t_r w100">
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
        <table-column-filter-dialog
          ref="tableColumnFilterDialog"
          :columns="columns"
          @handleFilterColumns="handleFilterColumns"
        ></table-column-filter-dialog>
      </el-form>
    </div>
    <!--表格内容栏-->
    <jic-table
      :height="350"
      :data="pageResult"
      :columns="filterColumns"
      @findPage="findPage"
      ref="JicTable"
      @handleEdit="handleEdit"
      @handleDelete="handleDelete"
      @handleCopy="handleCopy"
      @handleLower="handleLower"
      @grounding="grounding"
      :showSelection="false"
      :showOperation="true"
      :showCopyOperation="true"
      :showPushOperation=false
      :showPushOperations="true"
      :groundings="true"
      :showLowerOperation="true"
      :showBatchDelete="false"
      :operationWidth="400"
    ></jic-table>

  </div>
</template>
<script>
  import PopupTreeInput from "@/components/PopupTreeInput";
  import JicTable from "@/views/Core/JicTable";
  import KtButton from "@/views/Core/KtButton";
  import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
  import {format} from "@/utils/datetime";
  import axios from "axios";

  export default {
    components: {
      PopupTreeInput,
      JicTable,
      KtButton,
      TableColumnFilterDialog
    },
    data() {
      return {
        imgUpload: this.utils.getUpLoadHost(),
        size: "small",
        document_license: "",
        filters: {
          id: "",
          title: "",
          updateTimeStart: "",
        },

        imgs: "",
        fileList: {},
        columns: [],
        shoplist: [],
        filterColumns: [],
        selected: "",
        pageRequest: {pageNum: 1, pageSize: 10},
        pageResult: {},
        dialogImageUrl: "",

        operation: false, // true:新增, false:编辑
        dialogVisible: false, // 新增编辑界面是否显示
        editLoading: false,
        printPage: false,
        dataFormRules: {},
        pageType: this.$route.query.pageType,
      };
    },
    methods: {
      // 获取分页数据
      findPage: function (data) {
        /**
         * 子组件传过来的分页参数
         * data.start 当前页
         * data.limit 当前页总条数
         * */
        if (data != null && data.start != null) {
          if (data.start != null) {
            this.pageRequest.pageNum = data.start;
          }
          if (data.limit != null) {
            this.pageRequest.pageSize = data.limit;
          }
        }

        let this_ = this;
        if (this_.filters === undefined || this_.filters === null) {
          this_.filters = {};
        }
        this_.filters.platform = 1;
        this_.filters.start = this.pageRequest.pageNum;
        this_.filters.limit = this.pageRequest.pageSize;
        if (this_.pageType) {
          this_.filters.pageType = this_.pageType;
        }
        this.utils.request.getCmsPageList(this_.filters, function (res) {
          this_.$refs.JicTable.findPage({
            content: res.data.rows,
            total: res.data.total
          });
        });
      },

      //列表下载
      downloadExcel() {
        this.$confirm("确定下载列表文件?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            let filename = "省市县列表";
            this.$refs.CyTable.exportData(
              this.filters,
              this.filterColumns,
              filename
            );
          })
          .catch(() => {
          });
      },

      reset: function () {
        this.filters = {};
        this.findPage();
      },

      // 时间格式化
      dateFormat: function (row, column, cellValue, index) {
        return format(row[column.property]);
      },

      // 处理表格列过滤显示
      displayFilterColumnsDialog: function () {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true);
      },
      // 处理表格列过滤显示
      handleFilterColumns: function (data) {
        this.filterColumns = data.filterColumns;
        this.$refs.tableColumnFilterDialog.setDialogVisible(false);
      },
      // 处理表格列过滤显示
      initColumns: function () {
        this.columns = [
          {prop: "id", label: "ID", minWidth: 120},
          {prop: "title", label: "名称", minWidth: 120},

          {
            prop: "status",
            label: "状态",
            minWidth: 120,
            formatter: this.status
          },

        ];
        const temp = [];
        $.each(this.columns, function (key, val) {
          temp.push(val);
        });
        this.filterColumns = temp;
      },

      platform: function (row, column, cellValue, index) {
        if (cellValue === 1) {
          return "H5";
        } else if (cellValue === 2) {
          return "PC";
        }
      },
      status: function (row, column, cellValue, index) {
        if (cellValue === 0) {
          return "草稿";
        } else if (cellValue === 1) {
          return "上架";
        } else {
          return "下架";
        }
      },
      // 显示新增界面
      handleAdd: function (productId) {
        const this_ = this;
        const params = {platform: 1, pageType: this_.pageType || 1}; //h5平台页面
        this_.utils.request.addCmsPage(params, function (res) {
          if (res.code === "0000") {
            this_.$router.push({
              //              path: '/CmsPageTemplates/'
              path: "/MIndexProductShow",
              query: {pageId: res.data}
            });
          } else {
            this_.$message({message: "后台异常", type: "error"});
          }
        });
      },
      // 编辑
      handleEdit: function (row) {
        this.$router.push({
          path: "/MIndexProductShow",
          query: {pageId: row.row.id}
        });
      },
      // 复制
      handleCopy: function (row) {
        this.$confirm("确认复制选中记录吗？", "提示", {
          type: "warning"
        })
          .then(() => {
            const params = {};
            params.id = row.row.id;
            const this_ = this;
            this.utils.request.copyCmsPage(params, function (res) {
              if (res.code === "0000") {
                this_.$message({message: "操作成功", type: "success"});
                this_.findPage(null);
              } else {
                this_.$message({
                  message: "操作失败, " + res.msg,
                  type: "error"
                });
              }
            });
          })
          .catch(() => {
            console.log("3333333");
          });
      },
      // 发布
      /* handlePush: function(row) {
         this.$confirm(
           "确认发布选中记录吗？一旦发布,将替换已发布的展示页面",
           "提示",
           {
             type: "warning"
           }
         )
           .then(() => {
             const params = {};
             params.id = row.row.id;
             const this_ = this;
             this.utils.request.pushCmsPage(params, function(res) {
               if (res.code === "0000") {
                 this_.$message({ message: "操作成功", type: "success" });
                 this_.findPage(null);
               } else {
                 this_.$message({
                   message: "操作失败, " + res.msg,
                   type: "error"
                 });
               }
             });
           })
           .catch(() => {
             console.log("3333333");
           });
       },*/
      // 下架
      handleLower: function (row) {
        this.$confirm("确认下架选中记录吗？", "提示", {
          type: "warning"
        })
          .then(() => {
            const params = {};
            params.id = row.row.id;
            const this_ = this;
            this.utils.request.lowerCmspage(params, function (res) {
              if (res.code === "0000") {
                this_.$message({message: "操作成功", type: "success"});
                this_.findPage(null);
              } else {
                this_.$message({
                  message: "操作失败, " + res.msg,
                  type: "error"
                });
              }
            });
          })
          .catch(() => {
            console.log("3333333");
          });
      },
      // 上架
      grounding: function (row) {
        this.$confirm("确认上架选中记录吗？一旦上架,将替换已上架的展示页面", "提示", {
          type: "warning"
        })
          .then(() => {
            const params = {};
            params.id = row.row.id;
            params.pageType = 1;
            const this_ = this;
            this.utils.request.lowerSoldOut(params, function (res) {
              if (res.code === "0000") {
                this_.$message({message: "操作成功", type: "success"});
                this_.findPage(null);
              } else {
                this_.$message({
                  message: "操作失败, " + res.msg,
                  type: "error"
                });
              }
            });
          })
          .catch(() => {
            console.log("3333333");
          });
      },
      // 删除
      handleDelete: function (row) {
        const params = {};
        params.id = row.params[0].id;
        const this_ = this;
        this.utils.request.deleteCmsPage(params, function (res) {
          if (res.code === "0000") {
            this_.$message({message: "操作成功", type: "success"});
            this_.findPage(null);
          } else {
            this_.$message({message: "操作失败, " + res.msg, type: "error"});
          }
        });
      }
    },
    mounted() {
      this.findPage();
      this.initColumns();
      if (this.$route.query.pageType) {
        this.pageType = this.$route.query.pageType;
      }
    }
  };
</script>
<style lang="scss" scoped>
</style>
